<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>天天生鲜-商品详情</title>
<link rel="short icon" href="images/copylogo.png">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="css/details.css">
</head>

<body>
	<!-- 头部  -->
	<div class="header_con">
    	<div class="header">
    		<div class="welcome fl">欢迎来到天天生鲜</div>
    		<div class="fr" id="stu_name">
            	
             </div>  
    	</div>
    </div>
    
     <!-- 搜素框 -->
     <div class="search_bar clearfix">
        <a href="index.html" class="logo fl"><img src="images/logo.png"></a>
        <div class="search_con fl">
            <input type="text" placeholder="搜素商品" name="" class="input_text fl" id="inp">
            <input type="button" value="搜索" class="input_btn fr" onclick="find()">
        </div>
        <div class="guest_cart fr">
            <a href="front/page/cart.html" class="cart_name fl">我的购物车</a>
            <div class="goods_count fl" id="show_count">0</div>
        </div>
     </div>
     
    <!-- 全部商品分类 --> 
    <div class="navbar_con">
    	<div class="navbar clearfix">
        	<!-- 动态下拉框 -->
    		<div class="subnav_con fl">
            	<h1 class="fl">全商品分类</h1>
            	<span></span>
                <ul class="subnav" id="typeinfo">
                   
            	</ul>
            </div>
    		<ul class="navlist fl">
    			<li><a href="index.html">首页</a></li>
                <li class="interval">|</li>
                <li><a href="">手机生鲜</a></li>
                <li class="interval">|</li>
                <li><a href="">抽奖</a></li>
    		</ul>
    	</div>
    </div>
     
    <!-- 面包屑导航 --> 
    <div class="breadcrumb" id="breadcrumb">
     	<a href="javascript:void(0)">全部分类</a>
        <span>&gt;</span>
        <a href="javascript:void(0)" id="gtype"></a>
        <span>&gt;</span>
        <a href="javascript:void(0)">商品详情</a>
     </div>
     
    <!-- 商品展示 -->
    <div class="goods_details_con clearfix" id="goodsinfo">
    	
    </div>
    
    <!-- 商品详情 -->
    <div class="main_wrap clearfix">
		<div class="l_wrap fl clearfix">
			<div class="new_goods">
				<h3>新品推荐</h3>
				<ul>
					<li>
						<a href="#"><img src="images/goods/goods001.jpg"></a>
						<h4><a href="#">进口柠檬</a></h4>
						<div class="prize">￥3.90</div>
					</li>
					<li>
						<a href="#"><img src="images/goods/goods002.jpg"></a>
						<h4><a href="#">玫瑰香葡萄</a></h4>
						<div class="prize">￥16.80</div>
					</li>
				</ul>
			</div>
		</div>

		<div class="r_wrap fr clearfix">
			<ul class="detail_tab clearfix">
				<li class="active">商品介绍</li>
				<li>评论</li>
                <li>推荐</li>
			</ul>

			<div class="tab_content">
				<dl>
					<dt>商品详情：</dt>
					<dd id="descr"></dd>
				</dl>
			</div>

		</div>
	</div>
    
    <!-- 版权所有 -->
    <div class="footer">
    	<div class="foot_link">
        	<a href="#">关于我们</a> <span> | </span>
            <a href="#">联系我们</a> <span> | </span>
            <a href="#">招聘广告</a> <span> | </span>
            <a href="#">友情链接</a>
        </div>
    	<p>CopyRight &copy; 2019 嘟嘟嘟水果欢迎你的到来 All Rights Reserverd</p>
        <p>电话： </p>
    </div>
    <script src="js/jquery-3.4.1.min.js"></script>
	<script>
		var mno="null";
		
		//立即购买
		function buy(gno){
			var num = Number($("#nums").val());
			if(mno==null){
				alert("请先登录...");
				location.href="login.html";
				return;
			}
			location.href="front/page/pay.html?gno="+gno+"&num="+num+"#flag=false";
		}
		
		//加入购物车
		function add(mno,gno){
			var num = Number($("#nums").val());
			if(mno==null){
				alert("请先登录...");
				location.href="login.html";
				return;
			}
			$.post("cartInfo/checkCar", {mno:mno,gno:gno},function(data){
				if(data.length<=0){//购物车不存在该商品
					$.post("cartInfo/add", {mno:mno,gno:gno,num:num},function(data){
						if(data>0){//添加成功
							alert("新商品,添加成功...");
							geCarNnm(mno);
						}
					},"text");
				} else {//购物车中已经存在该商品,只需要加数量就行了
					$.post("cartInfo/update", {num:num,cno:data},function(data){
						if(data>0){
							alert("添加成功...");
						}
					},"text");
				}				
			},"text");
		}

		$.post("menberInfo/checkLogin", null , function(data){
			var str ='';
			
			if(!data.code){
				mno=data.mno;
				str+='<div class="login_info fl">';
				str+='欢迎：<em>'+data.nickName+'</em>';
				str+='</div> ';
				geCarNnm(mno);
			} else {			
				str+='<div class="login_btn fl">';
				str+='<a href="login.html">登录</a>';
				str+='<span> | </span>';
				str+='<a href="register.html">注册</a>';
				str+='</div>';
			}
			str+='<div class="user_link fl">';
			str+='<span> | </span>';
			str+='<a href="front/page/user.html">用户中心</a>';
			str+='<span> | </span>';
			str+='<a href="front/page/cart.html">我的购物车</a>';
			str+='<span> | </span>';
			str+=' <a href="front/page/order.html">我的订单</a>  </div> ';
	    	$("#stu_name").html(str);
		}, "json");
		
		//发送ajax请求，得到类别
	    $.post("goodsInfo/findAll", null ,function(data){
	    	var tno = data[0].tno; 
	    	var typestr='';
	    	var goodstr='';
	    	var arr=["fruit","seafood","meet","egg","vegetables","ice"];	    		
	    	if(location.href.split("?").length>=2 ){
				  //容错处理，不这样做的话，如果没有参数，那么我们的服务器就会直接报错
				  var no=location.href.split("?")[1].split("=")[1];				  
			}
	    	for(var i=0;i<data.length;i++){
	    		if(data[i].gno==no){
		    		$("#gtype").html(data[i].tname);
		    		$("#descr").html(data[i].descr);
		    		goodstr+='<div class="goods_details_pic fl"><img src="'+data[i].pics+'"></div>';
		    		goodstr+='<div class="goods_details_list fr">';
		    		goodstr+='<h3>'+data[i].gname+'</h3> <p>'+data[i].intro+'</p>';
		    		goodstr+='<div class="price_bar"> <span class="show_price">￥<em>'+data[i].price+'</em>元</span>';
		    		goodstr+='<span class="show_unit">单位：<em>500g</em></span>  </div>';
		    		goodstr+='<div class="goods_num clearfix">';
		    		goodstr+='<div class="num_name fl">数量：</div>';
		    		goodstr+='<div class="num_add fl">';
		    		goodstr+='<input type="text" class="num_show fl" value="1" id="nums">';
		    		goodstr+='<a href="javascript:;" class="add fr" onclick="update(1,'+data[i].price+','+data[i].balance+')">+</a>';
		    		goodstr+='<a href="javascript:;" class="minus fr" onclick="update(-1,'+data[i].price+','+data[i].balance+')">-</a>	</div>  </div>';
		    		goodstr+='<div class="total">总价：<em id="total">'+data[i].price+'</em>元</div>';
		    		goodstr+='<div class="operate_btn">';
		    		goodstr+='<a href="javascript:;" class="buy_btn" id="buy_btn" onclick="buy('+data[i].gno+')">立即购买</a>';
		    		goodstr+='<a href="javascript:;" class="add_cart" id="add_cart" onclick="add('+mno+','+data[i].gno+')">加入购物车</a>	 </div>  </div>';		    		
	    		}	    		
	    		if(data[i].tno == tno){
	    			typestr+='<li><a href="goods.html?tno='+tno+'" class="'+arr[i]+'">'+data[i].tname +'</a></li>';
	    		 	tno++;
	    	 	}	   
	    	}
	    	 $("#typeinfo").html(typestr);
	    	 $("#goodsinfo").html(goodstr);	    	 
	    },"json");
		
		function update(num, price, balance){
			//获取商品数量
			var nums = Number($("#nums").val());
			nums=num+nums;
			if(nums<1){
				return;
			}
			if(nums>balance){
				alert("超出库存...");
				return;
			}
			var total=nums*price*1.00;
			 $("#nums").attr('value', nums);
			 $("#total").html(total);	   
		}
		
		function geCarNnm(mno){
			$.post("cartInfo/finds", {mno:mno}, function(data){
				var numStr=data.length;		    	
				$("#show_count").html(numStr);
			},"json");	
		}
		
		function find(){
			var str=$("#inp").val();
			$.post("goodsInfo/find",{str:str},function(data){
				console.log(data);
				if(data==null){
					alert("抱歉,没有该商品...");
					console.log("抱歉,没有该商品...");
				}else{					
					location.href="details.html?gno="+data.gno+"#flag=false";
				}
			},"json");
		}
	</script>
</body>
</html>


